﻿<?php
    /*
     * Add data to Databases HaiHuocTheThao
     * Author   ThanhTV
     * Email    tranthanhepu@gmail.com
     * Date     Mar 06, 2013
     */

    define("index", true);
    error_reporting(1);

    /* Include file */
    include "conf/config.php";   
    include "db/mysql.php";	
    /*End include file*/		    	       
    
    if(isset($_POST) && $_POST != null){                
        
        //Define variables
        $str_error = "";
        $str_success = "";
        
        $str_name   = $_POST['name'];
        $str_desc   = $_POST['desc'];
        $str_img    = $_POST['image'];
        $str_url    = $_POST['url'];
        $str_type   = $_POST['type'];
        
        
        //Check valid data
        if($str_name == ""){
            $str_error = "Name not empty <br/>";
        }        
        if($str_img == ""){
            $str_error .= "Image not empty <br/>";
        }
        if($str_url == ""){
            $str_error .= "url not empty <br/>";
        }                
        
        //If not exits any error
        if($str_error == ""){
                        
            /*
             * Connect and Insert to database
             */
            $db = new database();
            $db -> db = array('host'=>$config['dbhost'], 'user'=>$config['dbuser'], 'pass'=>$config['dbpass'], 'db'=>$config['dbname']);
            $db -> connect();
            $db -> query("set names utf8");
            
            //Format input data
            $date_create = date('Y-m-d H:i:s');
            $str_name = strip_tags($str_name);            
            $str_img = strip_tags($str_img);
            $str_url = strip_tags($str_url);
            
            
            $result = $db->query("INSERT INTO `list_videos` SET `title`= '$str_name', `description` = '$str_desc', `thumb` = '$str_img', `url` = '$str_url',`type` = '$str_type' ,`date_create` = '$date_create' ");
            
            if($result){
                $str_success = "Insert data success!";
            }
            else{
                $str_error = "Insert data error!";
            }
            
            //Close connection to Database
            $db ->Close();
        }        
    }
    
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Add video to database</title>        
    <link type="text/css" rel="stylesheet" href="css/main.css" />
    <script type="text/javascript" src="js/main.js"></script>    
</head>

    <body>
        <div id="header">
            <div class="wrapper">
                <div class="form">
                                           
                    <?php if( count($str_error) > 0 ) { ?>
                        <div class="messages"><?php echo "<p>".$str_error."</p>";?></div>
                    <?php }?>
                        
                    <?php if( count($str_success) > 0 ) { ?>
                        <div class="messages"><?php echo "<h2>".$str_success."</h2><meta http-equiv=\"refresh\" content=\"2;URL='/add.php'\" />  ";?></div>
                    <?php }?>                        
                                                    
                    <form action="add.php" method="post">
                        <ul>
                            <li>
                                <h1>Name</h1>
                                <span><input name="name" id="name" value="<?php echo  $_POST['name'] != '' ? $_POST['name'] : '';?>"/></span>
                            </li>
                            <li>
                                <h1>Description</h1>
                                <span><textarea col="4" rows="4" name="desc" id="desc"><?php echo  $_POST['desc'] != '' ? $_POST['desc'] : '';?></textarea></span>
                            </li>
                            <li>
                                <h1>Images</h1>
                                <span><input name="image" value="<?php echo  $_POST['image'] != '' ? $_POST['image'] : '';?>"/></span>
                            </li>
                            <li>
                                <h1>Url</h1>
                                <span><input name="url" value="<?php echo  $_POST['url'] != '' ? $_POST['url'] : '';?>"/></span>
                            </li>
                            <li>
                                <h1>Type</h1>                                
                                <span>
                                    <input class="type" type="radio" name="type" value="0" <?php echo  $_POST['type'] == 0 ? 'checked="checked"' : '';?>>Video<br>
                                    <input class="type" type="radio" name="type" value="1" <?php echo  $_POST['type'] == 1 ? 'checked="checked"' : '';?>>Photo
                                </span>
                            </li>
                            <li>
                                <input type="submit" name="submit" value="Submit" id="submit"/>
                            </li>
                        </ul>            
                    </form>
                </div>    
            </div>    
        </div>
    </body>
</html>